package com.traffic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.traffic.entity.Bigdata;



import com.traffic.mapper.BigdataMapper;
import com.traffic.service.BigdataService;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.zip.DeflaterOutputStream;

@Service
public class BigdataServiceImpl extends ServiceImpl<BigdataMapper, Bigdata> implements BigdataService{

    @Autowired
    BigdataMapper bigdataMapper;

    @Override
    public int insertTypeData(List<Double> xs,int y, int type,Long userid) {

        Bigdata sd = new Bigdata();
        sd.setY(y);
        sd.setType(type);

        sd.setX1(xs.get(0));
        sd.setX2(xs.get(1));
        sd.setX3(xs.get(2));
        sd.setX4(xs.get(3));
        sd.setX5(xs.get(4));
        sd.setX6(xs.get(5));
        sd.setX7(xs.get(6));
        sd.setX8(xs.get(7));
        sd.setUserId(userid);

        System.out.println(sd);

        bigdataMapper.insert(sd);

        return 1;
    }

    @Override
    public Double TurnPercent(String col,String param, Long userid) {

        QueryWrapper<Bigdata> wrapper = new QueryWrapper<>();
        Object all = bigdataMapper.selectObjs(wrapper.select("COUNT(*)").last("WHERE user_id ="+userid.toString()+" and type=1"));

        Double num = Double.parseDouble(StringUtils.strip(all.toString(),"[]"));

        QueryWrapper<Bigdata> wrapper2 = new QueryWrapper<>();
        Object target = bigdataMapper.selectObjs(wrapper2.select("COUNT(*)").last("WHERE user_id ="+userid.toString()+" and "+col+"="+param+" and type=1"));
        Double tar = Double.parseDouble(StringUtils.strip(target.toString(),"[]"));

        return tar/num;
    }
}
